package com.easyjava.vuebuilder;

import com.easyjava.bean.Constants;
import com.easyjava.bean.FieldInfo;
import com.easyjava.bean.TableInfo;
import com.easyjava.utils.MyStringUtils;
import com.easyjava.vuebean.ConstantsVue;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author: 小闹心
 * @Description: 生成前端展示界面
 * @DateTime: 2024/11/22 18:51
 **/
public class BuilderLayout {
    public static final Logger logger = LoggerFactory.getLogger(BuilderLayout.class);
    public static void execute(List<TableInfo> tableInfoList){
            File folder = new File(ConstantsVue.PATH_VUE_BASE);
            if(!folder.exists()){
                folder.mkdirs();
            }
            File javaFile = new File(folder, "index-layout.vue");
            OutputStream out = null;
            OutputStreamWriter outWriter = null;
            BufferedWriter bw = null;
            try{
                out = new FileOutputStream(javaFile);
                outWriter = new OutputStreamWriter(out, StandardCharsets.UTF_8);
                bw = new BufferedWriter(outWriter);
                bw.write("<template>");
                bw.newLine();
                bw.write("\t<el-tabs tab-position=\"left\" style=\"width: 70vw;\" class=\"demo-tabs\">");
                bw.newLine();
                for(TableInfo tableInfo : tableInfoList){
                    bw.write("\t\t<el-tab-pane label=\"" + tableInfo.getComment() + "\"><" + tableInfo.getBeanName() + "/></el-tab-pane>");
                    bw.newLine();
                }
                bw.write("\t</el-tabs>");
                bw.newLine();
                bw.write("</template>");
                bw.newLine();
                bw.newLine();
                bw.write("<script setup>");
                bw.newLine();
                for(TableInfo tableInfo : tableInfoList){
                    bw.write("import " + tableInfo.getBeanName() + " from '." + ConstantsVue.PATH_VUE_SHOW_VIEWS
                            + "/" + MyStringUtils.lowerCaseFirstLetter(tableInfo.getBeanName()) + "-show.vue';");
                    bw.newLine();
                }
                bw.write("</script>");
                bw.newLine();
                bw.newLine();
                bw.write("<style lang=\"scss\">");
                bw.newLine();
                bw.write(".demo-tabs{");
                bw.newLine();
                bw.write("\t.el-tabs__header{");
                bw.newLine();
                bw.write("\t\t.el-tabs__item{ color: white; }");
                bw.newLine();
                bw.write("\t\t.is-active{ color: rgb(0, 194, 253); }");
                bw.newLine();
                bw.write("\t\t.el-tabs__item:hover{ color: rgb(236, 236, 137); }");
                bw.newLine();
                bw.write("\t}");
                bw.newLine();
                bw.write("}");
                bw.newLine();
                bw.write("</style>");
                bw.newLine();
                bw.flush();
            }catch (Exception e){
                logger.error("生成展示页面失败" );
            }finally {
                if(bw != null){
                    try {
                        bw.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(outWriter != null){
                    try {
                        outWriter.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(out != null){
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
    }
}
